package cn.gl.org.controller;

import cn.gl.basic.annotation.PreAuthorize;
import cn.gl.basic.util.AjaxResult;
import cn.gl.basic.util.PageList;
import cn.gl.org.domain.Department;
import cn.gl.org.domain.Shop;
import cn.gl.org.query.DepartmentQuery;
import cn.gl.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@ResponseBody
@Api(value = "部门接口类",description ="处理部门的CRUD操作" )
class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Department findOne(@PathVariable("id")Long id){
        return departmentService.laodOne(id);
    }
    @GetMapping
    @ApiOperation(value = "查询多个对象")
    public List<Department> findAll(){
        return departmentService.laodAll();
    }

    @PostMapping
    @ApiOperation(value = "分页高级查询")
    @PreAuthorize(name = "部门列表",sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }
    /**
     * 接口：删除
     * @param id
     * @return
     */
    //前端：delete http://localhost:8080/department/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");

        }
    }

    /**
     * 添加或修改
     * @param department
     * @return
     */
    //前端：put http://localhost:8080/department/ json对象{}
    @PutMapping
    @ApiOperation(value = "添加和修改")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.insert(department);
            }else{
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    @ApiOperation(value = "批量删除接口")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            //调用service
            departmentService.patchDelete(ids);
               return new AjaxResult();

        }catch(Exception e){
            e.printStackTrace();
            //return new AjaxResult(false,"删除失败");
            return  AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    @GetMapping("/deptTree")
    public List<Department>depttree(){
        return departmentService.deptTree();
    }

}